<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Service\FileService;
use App\Service\SubService\UserAuth;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Swagger\Annotation as SA;
use Hyperf\Swagger\Request\SwaggerRequest;

#[SA\HyperfServer('http')]
class FileController extends AbstractController
{
    #[Inject]
    protected FileService $fileService;

    #[SA\Post(path: '/file/upload', summary: '文件上传', tags: ['文件相关'])]
    #[SA\RequestBody(
        description: '请求参数',
        content: [
            new SA\MediaType(
                mediaType: 'multipart/form-data',
                schema: new SA\Schema(
                    required: ['file'],
                    properties: [
                        new SA\Property(property: 'file', description: '文件', type: 'file'),
                    ]
                ),
            ),
        ],
    )]
    #[SA\Response(response: '200', content: new SA\JsonContent(ref: '#components/schemas/FileSchema'))]
    public function upload(SwaggerRequest $request)
    {
        $file = $request->file('file');
        $user = UserAuth::instance();

        return $this->response->success($this->fileService->create('local', $file, $user));
    }
}
